package com.root.dreameraadmin.activity;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;

import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;

import com.root.dreameraadmin.R;
import com.root.dreameraadmin.utils.HttpConnectionUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;


public class PointInMapActivity extends AppCompatActivity {

    @Bind(R.id.ib_map)
    Button mapBtn;


    private static final String TAG = "PointInMapActivity";

    private MapView mMapView = null; //百度地图控件
    private BaiduMap bdMap; // 百度地图对象
    private BMapManager manager;
    public static LinearLayout map;//地图的容器
    BitmapDescriptor bitmap;
    private String longitude;
    private String latitude;
    private String name;
    private double input_longitude;
    private double input_latitude;
    FloatingActionButton fab;
    HashMap<String,Object> paramMap;

    private Button back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_point_in_map);
        ButterKnife.bind(this);
        mapBtn.setVisibility(View.GONE);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        init();
        fab = (FloatingActionButton) findViewById(R.id.fab);
        back= (Button) findViewById(R.id.ib_back);
    }


    private void init() {
        mMapView = (MapView) findViewById(R.id.id_bmapView);
        bdMap = mMapView.getMap();
        LatLng latLng = new LatLng(41.802273,123.417315);//将地图移至沈阳市
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(latLng);
        bdMap.animateMapStatus(u);
    }

    @Override
    public void onResume(){
        super.onResume();
        Log.d(TAG,"onResume");

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        latitude=bundle.getString("point_latitude");
        longitude=bundle.getString("point_longitude");
        name=bundle.getString("point_name");
        Log.d(TAG,"the lon and lat is "+latitude+","+longitude);
        ShowPoint(Double.parseDouble(latitude),Double.parseDouble(longitude),true);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your new position,Please click on the map", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                bdMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
                    @Override
                    public void onMapClick(LatLng latLng) {

                        Log.d(TAG,"omMapClick");
                        System.out.print("latitude+"+latLng.latitude+","+"longitude+"+latLng.longitude);
                        input_longitude=latLng.longitude;
                        input_latitude=latLng.latitude;
                        ShowPoint(input_latitude,input_longitude,false);

                        paramMap = new HashMap<String, Object>();
                        paramMap.put("name", name);
                        paramMap.put("longitude",String.valueOf(input_longitude));
                        paramMap.put("latitude",String.valueOf(input_latitude));
//                        paramMap.put("city","");

                        new AlertDialog.Builder(PointInMapActivity.this).setTitle("系统提示")//设置对话框标题
                                .setMessage("Update point by the new position?"+"Longitude :"+input_longitude+"Latitude :"+input_latitude)//设置显示的内容
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {//添加确定按钮
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                        //new thread to add
                                        new Thread() {
                                            public void run() {
                                                try{
                                                    HttpConnectionUtil.doPut(HttpConnectionUtil.PLACEURL, paramMap);
                                                    runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            if (HttpConnectionUtil.result == "success") {
                                                                Toast.makeText(PointInMapActivity.this, "修改成功!", Toast.LENGTH_SHORT).show();
                                                                paramMap.clear();
                                                            }
                                                            else{
                                                                Toast.makeText(PointInMapActivity.this, "修改失败!", Toast.LENGTH_SHORT).show();
                                                                paramMap.clear();
                                                            }
                                                        }
                                                    });
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }.start();
                                    }
                                })
                                .setNegativeButton("No", new DialogInterface.OnClickListener() {//添加返回按钮
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {//响应事件
                                        // TODO Auto-generated method stub
                                        dialog.dismiss();
                                    }
                                }).show();//在按键响应事件中显示此对话框
                    }
                    @Override
                    public boolean onMapPoiClick(MapPoi mapPoi) {
                        return false;
                    }
                });
            }


        });
        back.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }





    public void ShowPoint(Double latitude,Double longitude,boolean flag) {
                 // 定义marker坐标点
                LatLng point = new LatLng(latitude,longitude);
                if(flag) {
                    bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.ic_marker);
                }else {
                    bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.ic_marker_red);
                }
                //构建markerOption，用于在地图上添加marker
               OverlayOptions options = new MarkerOptions()//
                       .position(point)// 设置marker的位置
                       .icon(bitmap)// 设置marker的图标
                       .zIndex(9)// 設置marker的所在層級
                        .draggable(true);// 设置手势拖拽
              // 在地图上添加marker，并显示
               Marker marker1 = (Marker) bdMap.addOverlay(options);

            //构建文字Option对象，用于在地图上添加文字
            OverlayOptions textOption = new TextOptions()
                    .bgColor(0xAAFFFF00)
                    .fontSize(35)
                    .fontColor(0xFFFF00FF)
                    .text(name)
                    .rotate(-30)
                    .position(point);
            //在地图上添加该文字对象并显示
            bdMap.addOverlay(textOption);
            }




}
